﻿using System;
using WiMo.Games;
using WiMo.Games.Inputs;
using WiMo.Games.Drawables;

namespace Images
{
    /// <summary>
    /// Main Game Class.
    /// 
    /// An interface is used so we can swap out all the implementations for the
    /// different components of the engine.
    /// </summary>
    public class Game : IGame
    {
        private ISpriteImage _helloWiMoGFImage;
        private ISpriteImage _solidGraphic;
        private ISpriteImage _partialTransparency1;
        private ISpriteImage _partialTransparency2;
        private ITexture _backgroundImage;
        public IGameEngine Engine { get; set; }

        public Game(IGameEngine engine)
        {
            Engine = engine;

            //Setup the content location before LoadContent
            Engine.ContentManager.ContentLocation = "Content";
        }

        public void Initialize()
        {
            Engine.InputManager.MenuBarBackgroundColor = new Color(Color.Black, 0x7f);
            Engine.InputManager.SoftMenuFontColor = Color.White;
            Engine.InputManager.RightSoftButton = "Exit";
        }

        public void BeginRun(){ }
        public void CreateMenus() {}

        public void LoadContent()
        {
            //Load up the background image.
            _backgroundImage = Engine.ContentManager.GetTexture("Background");

            //Notes:
            //1) The Graphic must reside in the ContentDirectory as specified in the ContentManager
            //2) The Image BuildAction must be set as Embedded Resource 
            //3) Case must match since it is stored as a resource
            _helloWiMoGFImage = Engine.ContentManager.LoadImage("HelloWiMoGF");

            //Set the transparency color for the graphic.
            _helloWiMoGFImage.ColorKey = Color.White;

            //Positon supplied will be the center of the graphic.
            _helloWiMoGFImage.Origin = Origin.Center;

            //Center the position on the screen.
            _helloWiMoGFImage.Position = new PointF(Engine.Display.TargetSize.Width / 2, 100.0f);

            _helloWiMoGFImage.RenderStrategy = RenderStrategy.Quality;

            _solidGraphic = Engine.ContentManager.LoadImage("SolidGraphic");
            _solidGraphic.RenderStrategy = RenderStrategy.Standard;
            _solidGraphic.Origin = Origin.Center;
            _solidGraphic.Position = new PointF(Engine.Display.TargetSize.Width / 2, 50.0f);
            _solidGraphic.ColorKey = new Color(Color.White, 0xFF);

            _partialTransparency1 = Engine.ContentManager.LoadImage("PartialTransparency");
            _partialTransparency1.RenderStrategy = RenderStrategy.Standard;
            _partialTransparency1.Origin = Origin.Center;
            _partialTransparency1.ColorKey = new Color(Color.White, 0xbf);
            _partialTransparency1.Position = new PointF(Engine.Display.TargetSize.Width / 2, 150.0f);

            _partialTransparency2 = Engine.ContentManager.LoadImage("PartialTransparency");
            _partialTransparency2.RenderStrategy = RenderStrategy.Quality;
            _partialTransparency2.Origin = Origin.Center;
            _partialTransparency2.Position = new PointF(Engine.Display.TargetSize.Width / 2, 250.0f);
        }

        IInputManager _previous;
        public void Update(GameTime gameTime)
        {
            if (Engine.InputManager.WasPressed(_previous, Keys.RightSoftButton))
                Engine.Exit();

            _previous = Engine.InputManager.GetKeysState();
        }

        public void Draw(GameTime gameTime)
        {
            Engine.Display.BackgroundImage = _backgroundImage;
            Engine.Display.Add(_solidGraphic);
            Engine.Display.Add(_helloWiMoGFImage);
            Engine.Display.Add(_partialTransparency1);
            Engine.Display.Add(_partialTransparency2);
        }

        public bool CanExit()
        {
            return true;
        }
    }
}
